package com.teame.readbaby;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.teame.readbaby.dao.AddressDao;
import com.teame.readbaby.parser.AddressListParser;
import com.teame.readbaby.parser.BooleanParser;
import com.teame.readbaby.util.CommonUtil;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.util.CopyFileUtils;
import com.teame.readbaby.util.GloableParams;
import com.teame.readbaby.util.Logger;
import com.teame.readbaby.vo.AddressListInfo;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/**
 * 地址管理
 * 
 * @author dupengtao
 */
public class AddressManagerActivity extends BaseActivity {

	protected static final String TAG = "DetaileAddressActivity";

	private SimpleAdapter simpleAdapter;
	private ListView address_manage_list;
	private TextView address_manager_null_text;
	
	private List<AddressListInfo> list;
	private AddressDao dao;
	
	private String delurl;
	private listviewAdapter adapter;
	
	private TextView head_back_text;
	private TextView address_manager_add_text;

	/**
	 * 你懂的
	 */
	@Override
	protected void findViewById() {
		
		address_manage_list = (ListView) this.findViewById(R.id.address_manage_list);  
		address_manager_null_text = (TextView) this.findViewById(R.id.address_manager_null_text);
		head_back_text =(TextView) this.findViewById(R.id.head_back_text);
		address_manager_add_text=(TextView) this.findViewById(R.id.address_manager_add_text);
	}

	/**
	 * 加载布局文件
	 */
	@Override
	protected void loadViewLayout() {
		this.setContentView(R.layout.address_manage_activity);
	}

	/**
	 * 后台请求数据 大家都按照我这个方法的方式去写
	 */
	@Override
	protected void processLogic() {
		
		
		
		// 1,请求的url地址
		String url = ConstantValue.HOST + ConstantValue.DETAILEADDRESS;
		// 2,请求的参数，没有为null
		// 如果有参数 这样写
		// Map<String, String> params=new HashMap<String, String>();
		// params.put("version", "1");
		// 3.json数据解析器，大家需要按照自己获取json的结构自己编写，要继承baseParser
		// 4,回调函数

		this.getDataFromServer(url, null, new AddressListParser(),
				new DataCallback<List<AddressListInfo>>() {

					

					@Override
					public void processData(List<AddressListInfo> paramObject,
							boolean paramBoolean) {

						final File addressDBFile = new File(
								AddressManagerActivity.this.getFilesDir(),
								"citydatabase.db");
						File f = CopyFileUtils.copyAssetsFile(
								AddressManagerActivity.this, "citydatabase.db",
								addressDBFile.getAbsolutePath());
						if (addressDBFile.exists()
								&& addressDBFile.length() > 0) {
							Logger.i(TAG, "文件已经存在,不需要再次拷贝");
						} else {
							if (f == null) {
								Logger.i(TAG, "拷贝失败");
							} else {
								Logger.i(TAG, "拷贝成功");
							}
						}

						if (!paramBoolean || paramObject == null) {// 获取失败
							showNoAddress();// 显示没有地址
						} else {// 获取成功
							
							list=paramObject;
							
							
							dao = new AddressDao();
							
//							List<Map<String, String>> data = new ArrayList<Map<String, String>>();
//							for (AddressListInfo info : paramObject) {
//								Map<String, String> map = new HashMap<String, String>();
//								map.put("姓名", info.getName());
//								map.put("电话", info.getPhonenumber());
//								map.put("地址1", dao.getCityName(info.cityid, info.areaid)+info.getAreadetail());
//								data.add(map);
//							}
//							String[] from = { "姓名", "电话", "地址1" };
//							int[] to = { R.id.address_listitem_receiver_text, R.id.address_listitem_phone_text,
//									R.id.address_listitem_ads_text};
//							simpleAdapter = new SimpleAdapter(
//									AddressManagerActivity.this, data,
//									R.layout.address_manage_listitem, from, to);
							adapter = new listviewAdapter();
							address_manage_list.setAdapter(adapter);
						}

					}

				});
		
		delurl=ConstantValue.HOST+ConstantValue.ADDRESSDELETE;
		
	}
	
	private class listviewAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			
			ViewHolder holder =null;
			
			if(convertView==null){

				holder = new ViewHolder();
				convertView=View.inflate(context, R.layout.address_manage_listitem, null);
				holder.tv_name=(TextView)convertView.findViewById(R.id.address_listitem_receiver_text);
				holder.tv_tel=(TextView)convertView.findViewById(R.id.address_listitem_phone_text);
				holder.tv_address=(TextView)convertView.findViewById(R.id.address_listitem_ads_text);
				holder.b_updata=(TextView) convertView.findViewById(R.id.address_manage_update_btn);
				holder.b_delete=(TextView) convertView.findViewById(R.id.address_manage_delete_btn);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
//			map.put("姓名", info.getName());
//			map.put("电话", info.getPhonenumber());
//			map.put("地址1", dao.getCityName(info.cityid, info.areaid)+info.getAreadetail());
			AddressListInfo info = list.get(position);
			holder.tv_name.setText(info.getName());
			holder.tv_name.setText(info.getPhonenumber());
			holder.tv_name.setText(dao.getCityName(info.cityid, info.areaid)+info.getAreadetail());
			
			holder.b_delete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					AddressListInfo info = list.get(position);
					 Map<String, String> params=new HashMap<String, String>();
					 params.put("id", info.getId());
					getDataFromServer(delurl, params, new BooleanParser(), new DataCallback<Boolean>(){
						@Override
						public void processData(Boolean paramObject,
								boolean paramBoolean) {
							if(paramObject){
								CommonUtil.showInfoDialog(AddressManagerActivity.this, "删除成功"+position);
								list.remove(position);
								adapter.notifyDataSetChanged();
								
							}else{
								CommonUtil.showInfoDialog(AddressManagerActivity.this, "删除失败"+position);
							}
						}
					
					});
				}
			});
			
			holder.b_updata.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					AddressListInfo info = list.get(position);
					Intent intent = new Intent(AddressManagerActivity.this,AddressUpdataActivity.class);
					intent.putExtra("info", info);
					startActivity(intent);
					AddressManagerActivity.this.finish();
				}
			});
			
			return convertView;
		}
	}
	
	static class ViewHolder{
		TextView tv_name;
		TextView tv_tel;
		TextView tv_address;
		TextView b_updata;
		TextView b_delete;
		
	}

	private void showNoAddress() {
		address_manager_null_text.setVisibility(View.VISIBLE);
	}

	/**
	 * 设置监听器
	 */
	@Override
	protected void setListener() {
		
		head_back_text.setOnClickListener(this);
		address_manager_add_text.setOnClickListener(this);
		
		//
//		address_manage_list.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view,
//					 final int position, long id) {
//				TextView address_manage_update_btn= (TextView) view.findViewById(R.id.address_manage_update_btn);
//				address_manage_update_btn.setOnClickListener(new OnClickListener() {
//					
//					@Override
//					public void onClick(View v) {
//						Logger.i(TAG, "位置修改按钮"+position);
//					}
//				});
//			}
//		});
	}

	@Override
	protected void onClickEvent(View paramView) {
		switch (paramView.getId()) {
		case R.id.head_back_text:
			this.finish();
			break;
		case R.id.address_manager_add_text:
			//dpt:采用一个新方法
			Intent intent = new Intent();
			intent.setComponent(new ComponentName(this, AddAddressActivity.class));
			startActivity(intent);
			break;
		}
	}
	
	

}
